package com.zjjk.transfer.app;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zjjk.transfer.source.entity.SourceDataDictionary;
import com.zjjk.transfer.source.entity.SourceUserAddress;
import com.zjjk.transfer.source.service.SourceDataDictionaryService;
import com.zjjk.transfer.source.service.SourceUserAddressService;
import com.zjjk.transfer.target.entity.TargetCustomerAddress;
import com.zjjk.transfer.target.service.TargetCustomerAddressService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class CustomerAddressTransfer extends BaseTransfer<SourceUserAddress, TargetCustomerAddress, SourceUserAddressService, TargetCustomerAddressService> {
    
    @Autowired
    private SourceDataDictionaryService dataDictionaryService;
    
    @Override
    protected TargetCustomerAddress convert(SourceUserAddress source) {
        Integer deptId = getDeptId(source.getDepartment());
        if (deptId == null) {
            return null;
        }
        if (source.getCreateTime() == null) {
            source.setCreateTime(source.getUpdateTime());
        }
        TargetCustomerAddress target = new TargetCustomerAddress();
        BeanUtils.copyProperties(source, target);
        target.setId(Long.valueOf(source.getId()));
        target.setUserId(Long.valueOf(source.getUserId()));
        target.setDepartment(deptId);
        return target;
    }
    
    private Integer getDeptId(String value) {
        LambdaQueryWrapper<SourceDataDictionary> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SourceDataDictionary::getValue, value);
        wrapper.eq(SourceDataDictionary::getParentId, -7);
        SourceDataDictionary dept = dataDictionaryService.getOne(wrapper);
        if (dept != null) {
            return dept.getId();
        }
        return null;
    }
}
